package ct.zxd.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ct.zxd.bean.Dish;
import ct.zxd.bean.Love;

public class TLoveDao {
	public int insertLove(String userId,Integer dishId) {
		Connection conn = null ;
		PreparedStatement pstm = null ;
		int count = 0 ;
		try {
			conn = DBconn.getconn();
			String sql = "insert into love(userid,dishid) values(?,?);" ;
			pstm = conn.prepareStatement(sql) ;
			pstm.setString(1, userId);
			pstm.setInt(2, dishId);
			count = pstm.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(!(conn == null)) {
				DBconn.closeconn(conn);
			}
		}
		return count;
	}
	
	
	public int deleteLoveByUserIdAndDishId(String userId,Integer dishId) {
		Connection conn = null ;
		PreparedStatement pstm = null ;
		int count = 0 ;
		try {
			conn = DBconn.getconn();
			String sql = "delete from love where userid = ? and dishid = ?;" ;
			pstm = conn.prepareStatement(sql) ;
			pstm.setString(1, userId);
			pstm.setInt(2, dishId);
			count = pstm.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(!(conn == null)) {
				DBconn.closeconn(conn);
			}
		}
		return count;
	}
	

	public List<Love> selectLoveByUserId(String userId) {
		Connection conn = null ;
		PreparedStatement pstm = null ;
		List<Love> loveList = new ArrayList<Love>();
		try {
			conn = DBconn.getconn();
			String sql = "select userid ,dishid from love where userid = ? ;" ;
			pstm = conn.prepareStatement(sql) ;
			pstm.setString(1, userId);
			ResultSet rs = pstm.executeQuery();
			while(rs.next()) {
				Love vo = new Love();
//				vo.setUserId(rs.getString(1));
//				vo.setDishId(rs.getInt(2));
				vo.setDish(this.selectDishByid(rs.getInt(2)));
				loveList.add(vo);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(!(conn == null)) {
				DBconn.closeconn(conn);
			}
		}
		return loveList;
	}
	
	public Dish selectDishByid(Integer id) {
		Connection conn = null ;
		PreparedStatement pstm = null ;
		Dish vo = null ;
		try {
			conn = DBconn.getconn();
			String sql = "select id,name,imageURL,price, stockNumber,"
					+ "shop_id,category_name,minusStatuses,quantity,introduce,category_id from dish where id = ? " ;
			pstm = conn.prepareStatement(sql) ;
			pstm.setInt(1, id);
			ResultSet rs = pstm.executeQuery();
			while(rs.next()) {
				vo = new Dish();
				vo.setDish_id(rs.getInt(1));
				vo.setName(rs.getString(2));
				vo.setImageURL(rs.getString(3));
				vo.setPrice(rs.getFloat(4));
				vo.setStockNumber(rs.getInt(5));
				vo.setShop_id(rs.getInt(6));
				vo.setCategory_name(rs.getString(7));;
				vo.setMinusStatuses(rs.getString(8));
				vo.setQuantity(rs.getInt(9));
				vo.setIntroduce(rs.getString(10));
				vo.setCategory_id(rs.getString(11));			
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(!(conn == null)) {
				DBconn.closeconn(conn);
			}
		}
		return vo;
	}
	
	public boolean isLoveByDishIdAndUserId(String userId , Integer dishId) {
		Connection conn = null ;
		PreparedStatement pstm = null ;
		boolean isLove = false;
		try {
 			conn = DBconn.getconn();
 			String sql = "select userid,dishid from love where userid = ? and dishid = ?" ;
 			pstm = conn.prepareStatement(sql) ;
 			pstm.setString(1,userId);
 			pstm.setInt(2,dishId);
 			ResultSet rs = pstm.executeQuery();
 			isLove = rs.next();

		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(!(conn == null)) {
				DBconn.closeconn(conn);
			}
		}
		
		return isLove;
	}
	
}
